package com.slmxz.tms.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Driver;
import com.slmxz.tms.document.model.vo.DriverVO;
import com.slmxz.tms.document.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 司机信息前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "司机信息接口")
@RestController
@RequestMapping("/document/driver")
@RequiredArgsConstructor
public class DriverController  {

    private final DriverService driverService;

    @Operation(summary = "司机信息分页列表")
    @GetMapping("/page")
    public PageResult<DriverVO> selectDriverPage(Query query, DriverVO driver) {
        IPage<DriverVO> result = this.driverService.selectDriverPage(Condition.getPage(query),driver);
        return PageResult.success(result);
    }

    @Operation(summary = "司机信息的下拉框")
    @GetMapping("/selectOption")
    public Result<?> selectOption(DriverVO driver){
        return Result.success(this.driverService.selectOption(driver));
    }

    @Operation(summary = "新增司机信息")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:driver:add')")
    public Result<Void> saveDriver(@RequestBody @Valid DriverVO  driver) throws Exception {
        boolean result = this.driverService.saveDriver(driver);
        return Result.result(result);
    }


    @Operation(summary = "获取司机信息详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:driver:edit')")
    public Result<Driver> selectDriverDetail(
        @Parameter(description = "司机信息ID") @PathVariable Long id
    ) {
        Driver driver = this.driverService.selectDriverDetail(id);
        return Result.success(driver);
    }

    @Operation(summary = "修改司机信息")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:driver:edit')")
    public Result<Void> updateDriver(
            @RequestBody @Validated DriverVO driver
    ) {
        boolean result = driverService.updateDriver(driver);
        return Result.result(result);
    }

    @Operation(summary = "删除司机信息")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:driver:delete')")
    public Result<Void> deleteDrivers(
        @Parameter(description = "司机信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.driverService.deleteDrivers(ids);
        return Result.result(result);
    }



    @Operation(summary = "激活或者注销司机信息")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody DriverVO driver) {
        boolean result = this.driverService.activeOrLogout(driver);
        return Result.result(result);
    }
}
